<?php

namespace App\Http\Controllers\Develop;

use Illuminate\Http\Request;

use App\Models\User\Seqno;

use App\Http\Controllers\Controller;

class SeqnoController extends Controller
{
    public function index()
    {
        return view ('develop/seqnoList');
    }

    //流水号列表
    public function search(Request $request)
    {
        $where = [];

        $name = $request->input('name');
        $remark = $request->input('remark');

        if (!empty($name)) {
            $where[] = ['name',$name];
        }
        if (!empty($remark)) {
            $where[] = ['remark',$remark];
        }

        $seqno_data = Seqno::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $return = [
            'total' => $seqno_data['total'],
            'rows' => []
        ];

        if ( $seqno_data['data'] ) {

            foreach( $seqno_data['data'] as $row ){

                $operation = '';
                $operation .='<a href="javascript: void(0);" onclick="seqno.del(\'' . $row['name'] . '\')">删除</a>&nbsp;&nbsp;
                            <a href="javascript: void(0);" onclick="seqno.edit(\'' . $row['name'] . '\')">修改</a>&nbsp;&nbsp;';

                $return['rows'][] = [
                    'operate' => $operation,
                    'name' => $row['name'],
                    'seqno' => $row['seqno'],
                    'remark' => $row['remark']
                ];

            }

        }

        return response()->json($return);

    }

    //获取单条流水号信息
    public function get($name)
    {

        $seqno_one = Seqno::find($name);

        return response()->json(['code'=>200, 'message'=>'ok', 'data'=>$seqno_one]);

    }

    //保存流水号信息
    public function store(Request $request)
    {

        $name = $request->input('name');
        $seqnoID = $request->input('seqnoID');
        $remark = $request->input('remark');

        if ( empty($name) ) {
            return response()->json(array(
                'code' => 100001,
                'message' => '流水号名称不能为空'
            ));
        }

        if ( !isset($seqnoID) ) {
            return response()->json(array(
                'code' => 100002,
                'message' => '流水号不能为空'
            ));
        }

        if ( empty($remark) ) {
            return response()->json(array(
                'code' => 100003,
                'message' => '备注不能为空'
            ));
        }
        $seqno = Seqno::find($name);

        if ( !$seqno ) {
            $seqno = new Seqno();
        }

        $seqno->name = $name;
        $seqno->seqno = $seqnoID;
        $seqno->remark = $remark;

        $seqno->save();

        return response()->json(['code' => 200, 'message' => '保存成功']);
    }

    //删除流水号信息
    public function delete($name)
    {

        $seqno = Seqno::find($name);

        if ( $seqno ) {
            $seqno->delete();
        }

        return response()->json(['code' => 200, 'message' => '删除成功']);

    }
}
